package com.yuncheng.spcyApi.controller;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.scheduleJob.ScheduleJobService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.entity.SpcySmsLog;
import com.yuncheng.spcyApi.service.ISpcySmsLogService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import utils.DateConversionUtils;

import java.util.Date;

/**
 * @Description: 审评查验-短信发送记录
 * @Author: yuncheng
 * @Date: 2023-05-24
 * @Version: V1.0
 */
@Api(tags = "审评查验-短信发送记录接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcySmsLog")
public class SpcySmsLogController {

    private static final Logger log = LoggerFactory.getLogger(SpcySmsLogController.class);

    @Resource
    private ISpcySmsLogService spcySmsLogService;

    @Resource
    private ScheduleJobService scheduleJobService;



    /**
     * 分页列表查询
     *
     * @param spcySmsLog
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Log(title = "短信发送记录-分页列表查询")
    @ApiOperation(value = "短信发送记录-分页列表查询")
    @GetMapping(value = "/queryPageList")
    public HttpResult<?> queryPageList(SpcySmsLog spcySmsLog,
                                       @RequestParam(value = "startTime",defaultValue = "")String startTime,
                                       @RequestParam(value = "endTime",defaultValue = "")String endTime,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       @RequestParam(name = "onlyCurrentUser",defaultValue = "true") boolean onlyCurrentUser) {
        // 判断是否查询当前登录人
        if (onlyCurrentUser){
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser == null){
                return HttpResult.error("当前登录已失效，请重新登录");
            }
            spcySmsLog.setFuserid(currentUser.getId());
        }

        Page<SpcySmsLog> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcySmsLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(spcySmsLog.getFdxnr()),SpcySmsLog::getFdxnr,spcySmsLog.getFdxnr());
        queryWrapper.like(StringUtils.isNotBlank(spcySmsLog.getFuserid()),SpcySmsLog::getFuserid,spcySmsLog.getFuserid());
        queryWrapper.like(StringUtils.isNotBlank(spcySmsLog.getFzdydx()),SpcySmsLog::getFzdydx,spcySmsLog.getFzdydx());
        queryWrapper.like(StringUtils.isNotBlank(spcySmsLog.getFdxlx()),SpcySmsLog::getFdxlx,spcySmsLog.getFdxlx());

        if (StringUtils.isNotBlank(spcySmsLog.getFjsdx())) {
            queryWrapper.and(q -> q.and(q1 -> q1.like(StringUtils.isNotBlank(spcySmsLog.getFjsdx()), SpcySmsLog::getFjsdx, spcySmsLog.getFjsdx()))
                    .or(q2 -> q2.like(StringUtils.isNotBlank(spcySmsLog.getFjsdx()), SpcySmsLog::getFzdydx, spcySmsLog.getFjsdx()))
            );
        }

        // 发送状态（全部、未发送、已发送）
        if (StringUtils.isNotBlank(spcySmsLog.getFfszt()) && !SpcyConstant.SMS_FSZT_ALL.equals(spcySmsLog.getFfszt())) {
            queryWrapper.like(StringUtils.isNotBlank(spcySmsLog.getFfszt()), SpcySmsLog::getFfszt, spcySmsLog.getFfszt());
        }

        if (StringUtils.isNotBlank(startTime)){
            Date kssj = DateConversionUtils.convertStringToDateYmdHms(startTime);
            queryWrapper.ge(SpcySmsLog::getFfssj, kssj);
        }
        if (StringUtils.isNotBlank(endTime)){
            Date jssj = DateConversionUtils.convertStringToDateYmdHms(endTime);
            queryWrapper.le(SpcySmsLog::getFfssj, jssj);
        }

        queryWrapper.orderByDesc(SpcySmsLog::getFfssj);
        queryWrapper.orderByDesc(SpcySmsLog::getCreateTime);
        IPage<SpcySmsLog> pageList = spcySmsLogService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    /**
     * 添加
     *
     * @param spcySmsLog
     * @return
     */
    @Log(title = "短信发送记录-添加")
    @ApiOperation(value = "短信发送记录-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody SpcySmsLog spcySmsLog) {
        if (StringUtils.isBlank(spcySmsLog.getFdxnr())){
            return HttpResult.error("短信内容不能为空");
        }
        boolean isExistJsdx = false;
        if (StringUtils.isBlank(spcySmsLog.getFjsdx())){
            isExistJsdx = false;
        }else {
            isExistJsdx = true;
        }
        if (!isExistJsdx && StringUtils.isBlank(spcySmsLog.getFzdydx())){
            isExistJsdx = false;
        }else {
            isExistJsdx = true;
        }
        if (!isExistJsdx){
            return HttpResult.error("接收对象/自定义对象,必须填写其中一个");
        }

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("当前登录已失效，请重新登录");
        }

        spcySmsLog.setFuserid(currentUser.getId());
        spcySmsLog.setFusername(currentUser.getName());

        if (StringUtils.isBlank(spcySmsLog.getId())){
            spcySmsLog.setId(GetUuIdUtils.ReplaceUuId());
            spcySmsLogService.save(spcySmsLog);
        }else {
            spcySmsLogService.updateById(spcySmsLog);
        }

        return HttpResult.ok("保存成功");
    }

    @Log(title = "短信发送记录-直接发送")
    @ApiOperation(value = "短信发送记录-直接发送")
    @PostMapping(value = "/send")
    public HttpResult<?> send(@RequestBody SpcySmsLog spcySmsLog) {
        if (StringUtils.isBlank(spcySmsLog.getFdxnr())){
            return HttpResult.error("短信内容不能为空");
        }
        boolean isExistJsdx = true;
        if (StringUtils.isBlank(spcySmsLog.getFjsdx())){
            isExistJsdx = false;
        }

        if (!isExistJsdx && StringUtils.isBlank(spcySmsLog.getFzdydx())){
            isExistJsdx = false;
        }else {
            isExistJsdx = true;
        }
        if (!isExistJsdx){
            return HttpResult.error("接收对象/自定义对象,必须填写其中一个");
        }

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("当前登录已失效，请重新登录");
        }

        spcySmsLog.setFuserid(currentUser.getId());
        spcySmsLog.setFusername(currentUser.getName());

        scheduleJobService.initScheduleSms(spcySmsLog); // 初始化-定时任务-发送短信

        return HttpResult.ok("发送成功");
    }

    /**
     * 编辑
     *
     * @param spcySmsLog
     * @return
     */
    @Log(title = "短信发送记录-编辑")
    @ApiOperation(value = "短信发送记录-编辑")
    @PostMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody SpcySmsLog spcySmsLog) {
        if (StringUtils.isBlank(spcySmsLog.getId())){
            return HttpResult.error("主要参数丢失，请联系管理员");
        }
        if (StringUtils.isBlank(spcySmsLog.getFdxnr())){
            return HttpResult.error("短信内容不能为空");
        }
        boolean isExistJsdx = false;
        if (StringUtils.isBlank(spcySmsLog.getFjsdx())){
            isExistJsdx = false;
        }else {
            isExistJsdx = true;
        }
        if (!isExistJsdx && StringUtils.isBlank(spcySmsLog.getFzdydx())){
            isExistJsdx = false;
        }else {
            isExistJsdx = true;
        }
        if (!isExistJsdx){
            return HttpResult.error("接收对象/自定义对象,必须填写其中一个");
        }
        spcySmsLogService.updateById(spcySmsLog);

        return HttpResult.ok("修改成功");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @Log(title = "短信发送记录-通过id删除")
    @ApiOperation(value = "短信发送记录-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        spcySmsLogService.removeById(id);

        return HttpResult.ok("删除成功");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Log(title = "短信发送记录-通过id查询")
    @ApiOperation(value = "短信发送记录-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        SpcySmsLog spcySmsLog = spcySmsLogService.getById(id);
        return HttpResult.ok(spcySmsLog);
    }

}
